﻿using System;
using System.Xml.Linq;

namespace Smarly.Idml.Package
{
	public interface IPackageFile
	{
		string Folder { get; }
		string FileName { get; }
		string ToResultString();
	}

	public abstract class PackageFile : IPackageFile
	{
		private string _declaration;
		private XDocument _document;

		protected string Declaration
		{
			get
			{
				if (_declaration == null)
				{
					var utf8 = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("root"));
					_declaration = utf8.Declaration + Environment.NewLine;
				}
				return _declaration;
			}
		}

		protected virtual string Text
		{
			get { return string.Empty; }
		}

		public virtual XDocument Document
		{
			get { return _document ?? (_document = XDocument.Parse(Text)); }
		}

		public abstract string Folder { get; }
		public abstract string FileName { get; }

		public virtual string ToResultString()
		{
			return Declaration + Document.ToString(SaveOptions.DisableFormatting);
		}
	}
}